package net.tngou.iask.action.my;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Address;
import net.tngou.iask.pojo.Main;
import net.tngou.iask.service.AddressService;
import net.tngou.iask.service.MainService;

/**
 * 地址
 * @author tngou@tngou.net
 *
 */
public class AddressAction extends BaseAction {

	/**
	 * 列表
	 */
	public void list() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		int type=(int) ask.getId(); //类型：1（国）、2（省）、3（市） 4、县 
		AddressService service = new AddressService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), type, ask.getKeyword());
		root.put("page", page);
		root.put("type", type);
		printFreemarker("my/address_list.ftl", root);
	}
	
	/**
	 * 显示
	 */
	public void show() {
		if(user.getRole()!=1&&user.getRole()!=2)
		{
			run_500();return;
		}
		int id=(int) ask.getId();
		if(id==0){run_404();return;}
		Address address = new Address().get(id);
		if(address==null){run_404();return;}
		List<?> childrens = service.getList("parent_id="+address.getId(), Address.class);
		root.put("address", address);
		root.put("childrens", childrens);
		List<Address> list = new ArrayList<>();
		int type=0;
		do {
			if(address!=null){
			  list.add(0,address);
			  type=address.getType();
			  address=new Address().get(address.getParent_id());
			}
		} while (type>1);
		
		root.put("list", list);
		printFreemarker("my/address_show.ftl", root);
		
	}
}
